package com.aliyun.messagemgt.dto.vo;

import com.aliyun.messagemgt.common.dto.base.PageQuery;
import com.aliyun.messagemgt.dto.SmsQuota;
import com.aliyun.messagemgt.dto.SmsQuotaMonitor;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.Max;
import java.util.Date;

/**
 * 短信配额请求参数VO
 *
 * @author byp
 * @date 2020/7/10
 */
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "SmsQuotaRequestVO", description = "短信配额参数信息")
@Data
@Accessors(chain = true)
public class SmsQuotaRequestVO extends PageQuery {

    @ApiModelProperty(value = "消息源UUID", name = "msgSourceId")
    private String msgSourceId;
    @ApiModelProperty(value = "短信配额类型", name = "quotaType", example = "1-年度 2-月度")
    private Integer quotaType;
    @ApiModelProperty(value = "配额包类型", name = "quotaPackageType", example = "1-固定配额包 2-自定义配额包")
    private Integer quotaPackageType;
    @ApiModelProperty(value = "配额", name = "quota")
    private Integer quota;
    @ApiModelProperty(value = "配额包数量,最大为10", name = "quotaPackageNum")
    @Max(10)
    private Integer quotaPackageNum;
    @ApiModelProperty(value = "生效类型", name = "effectiveType", example = "1-立即生效 2-次月/次年生效")
    private Integer effectiveType;
    @ApiModelProperty(value = "触发告警配额余量", name = "monitorQuota", example = "100000")
    private Integer monitorMarginQuota;
    @ApiModelProperty(value = "触发续订配额余量", name = "renewMarginQuota", example = "5000")
    private Integer renewMarginQuota;
    @ApiModelProperty(value = "续订配额量，单位：千条", name = "renewQuota", example = "5")
    private Integer renewQuota;
    @ApiModelProperty(value = "生效状态", name = "effectiveStatus", example = "1-已生效 2-未生效 3-失效")
    private Integer effectiveStatus;

    @ApiModelProperty(value = "归属部门编码", example = "12345")
    private String orgCode;
    @ApiModelProperty(value = "配置时间排序字段", example = "asc-正序 desc-倒叙")
    private String updateTimeOrderBy;
    @ApiModelProperty(value = "列表页查询字段（可以使消息源名称或渠道名称）", example = "XXX系统消息源")
    private String selfQueryParam;
    @ApiModelProperty(value = "配置时间查询起始时间", example = "2020-01-01 00:00:00")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date updateTimeBegin;
    @ApiModelProperty(value = "配置时间查询截止时间", example = "2020-02-01 00:00:00")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date updateTimeEnd;

    /**
     * 消息源的审核状态 2未提交 0 未审核 -1 审核未通过 1 审核通过
     */
    @ApiModelProperty(value = "发布状态（消息源的审核状态）", name = "auditStatus", example = "0-待审批 -1-未通过 1-已发布 2-未发布")
    private Integer auditStatus;


    public SmsQuota convertToSmsQuota() {
        VoConvertSmsQuota smsQuotaVoConvert  = new VoConvertSmsQuota();
        SmsQuota convert = smsQuotaVoConvert.convert(this);
        return convert;
    }

    public SmsQuotaMonitor convertToSmsQuotaMonitor() {
        if (this.getMonitorMarginQuota() == null || this.getMonitorMarginQuota() <= 0) {
            //说明是取消监控，或者说第一次修改消息源短信配额没有添加监控。
            return null;
        }
        VoConvertSmsQuotaMonitor vo = new VoConvertSmsQuotaMonitor();
        SmsQuotaMonitor convert = vo.convert(this);
        if (convert.getMonitorQuota() == null) {
            convert.setMonitorQuota(this.getMonitorMarginQuota());
        }
        convert.setCreateTime(new Date());
        return convert;
    }

    /**
     * VO转换为保存短信配额对象SmsQuota
     */
    private static class VoConvertSmsQuota implements Converter<SmsQuotaRequestVO, SmsQuota> {
        @Override
        public SmsQuota convert(SmsQuotaRequestVO smsQuotaRequestVO) {
            SmsQuota smsQuota = new SmsQuota();
            BeanUtils.copyProperties(smsQuotaRequestVO, smsQuota);
            return smsQuota;
        }
    }

    /**
     * VO转换为SmsQuotaMonitor
     */
    private static class VoConvertSmsQuotaMonitor implements Converter<SmsQuotaRequestVO, SmsQuotaMonitor> {

        @Override
        public SmsQuotaMonitor convert(SmsQuotaRequestVO source) {
            SmsQuotaMonitor smsQuotaMonitor = new SmsQuotaMonitor();
            BeanUtils.copyProperties(source, smsQuotaMonitor);
            return smsQuotaMonitor;
        }
    }

}
